package cn.hl.hm.service.impl;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.vizhuo.client.base.AbstractReturnCodeConstant;
import com.vizhuo.client.business.smscode.constant.SMSCodeConstant;
import com.vizhuo.client.business.smscode.reply.PostSMSCodeReply;
import com.vizhuo.client.business.smscode.request.PostSMSCodeRequest;
import com.vizhuo.client.business.smscode.request.PostSMSMessageRequest;
import com.vizhuo.client.business.smscode.returncode.SMSCodeReturnCode;
import com.vizhuo.smscode.model.SMSCode;
import com.vizhuo.utils.CreateRandomVcode;

import cn.hl.hm.dao.SMSCodeIMapping;
import cn.hl.hm.service.SMSCodeIService;
import smsClient.SingletonClient;
import smsClient.TestSDKClient;

/**
 * smscode
 * 
 * @author xujj
 * @version 1.0
 */

@Service("sMSCodeIService")
public class SMSCodeServiceImpl implements SMSCodeIService {
    private static Logger logger = LoggerFactory.getLogger(SMSCodeServiceImpl.class);

    @Autowired
    private SMSCodeIMapping sMSCodeIMapper;
    @Override
    public SMSCode findCode(SMSCode sMSCode) {

        return sMSCodeIMapper.selectCode(sMSCode);
    }

    
    
    @Override
    public PostSMSCodeReply addSMSCode(PostSMSCodeRequest request) throws Exception {
        PostSMSCodeReply reply = new PostSMSCodeReply();
        
        // 首先判断该手机号码是否在数据库记录的号段中，不在，则提示非法手机号码
//        String mobileOperator = mobDefenceIService.getOperater(request.getMobile());
//        
//        if (MobilePhoneOperatorUtil.UNKNWON.equals(mobileOperator)) {// 无效手机号
//            reply.setReturnCode(SMSCodeReturnCode.UNKNOW_MOBILE);
//            return reply;
//        }
        
       logger.info("-------------------getType："+request.getType());

        // 验证是注册还是找回，注册占辉已判断，只需加找回密码的验证即可
        if (SMSCodeConstant.GET_BACK.equals(request.getType())) {
            // 验证数据库是否存在，存在则发送，不存在则返回该账号不存在
            int n = 1 ;//mebAccIMapper.selectMebaccNumByAccount(request.getMobile());
            if (n >= 1) {
                // 存在，则发验证码

            } else {
                // 不存在，直接返回不存在
                reply.setStateCode(SMSCodeReturnCode.ACCOUNT_NOT_EXIST);
                return reply;
            }
        }
        // 首先向数据库插入验证码
        String code = CreateRandomVcode.createRandomVcode(true, 4);
        //String code = "1234";
        SMSCode smscode = new SMSCode(request.getMobile(), code,request.getType());
        sMSCodeIMapper.insertEntity(smscode);
        int i = 0;
//        		SingletonClient.getClient().sendSMS(new String[] { request.getMobile() },
//                "【华凌叮当到】您的验证码为:" + code + "，有效输入时间为30分钟。如有疑问，请致电客服热线：400-998-3903。", "", 5);// 带扩展码
        
        SendSmsRequest smsRequest = new SendSmsRequest();
		smsRequest.setTemplateParam("{\"code\":\""+code+"\"}");
	    TestSDKClient.sendSmsByAli("SMS_110535050",smsRequest,request.getMobile(),"华凌叮当到");

        
        logger.info(request.getMobile() + "第三方服务器返回的状态码为:" + i + ",验证码为" + code);
        if (i == 0) {
            reply.setStateCode(AbstractReturnCodeConstant.SYS_SUCCESS);
            reply.setStateMessage(AbstractReturnCodeConstant.messageMap.get(AbstractReturnCodeConstant.SYS_SUCCESS));
        } else {
            reply.setStateCode(i + "");
        }
        return reply;
    }

    @Override
    public PostSMSCodeReply postSMSMessage(PostSMSMessageRequest request) throws Exception {
        PostSMSCodeReply reply = new PostSMSCodeReply();
        // 内容格式可以是这种
        // "【华凌叮当到】您的朋友邀请您下载<聚牛物流>手机App,请点击这里查看:http://10.0.0.235/app/html/download_test.html"
        int i = SingletonClient.getClient().sendSMS(request.getMobiles(), request.getMessage(), "", 5);// 带扩展码
        logger.info("第三方服务器返回的状态码为:" + i);
        if (i == 0) {
            reply.setStateCode(AbstractReturnCodeConstant.SYS_SUCCESS);
            reply.setStateMessage(AbstractReturnCodeConstant.messageMap.get(AbstractReturnCodeConstant.SYS_SUCCESS));
        } else {
            reply.setStateCode(i + "");
        }
        return reply;
    }

}
